package sortingRMI_client;

import java.util.Random;

@SuppressWarnings("unchecked")
public class ArrayFiller {
    private static final int _STRING_BANDWIDTH = 10;
    private static final int _CHAR_BANDWIDTH = 26;
    private static final int _INTEGER_BANDWIDTH = 10000;
    private static final int _INTEGER_CD_BANDWIDTH = 32;

    public ArrayFiller() {
    }
    
    public Comparable[] generateRandomList(int length, Class type) 
    { 
        Comparable[] unsortedComparableArray = null;
        String s = type.getName();
        
        if (s.equals("int")) {
            unsortedComparableArray = generateRandomInt(length);
        }
        if (s.equals("char")) {
            unsortedComparableArray = generateRandomChar(length);
        }
        if (s.equals("java.lang.String")) {
            unsortedComparableArray = generateRandomString(length);
        }
        if (s.equals("sortingRMI_client.MuziekCD")) {
            unsortedComparableArray = generateRandomMuziekCD(length);
        }
        
        return unsortedComparableArray;
    }
    
    public Comparable[] generateRandomInt(int length) {
        // generate integers on demand
    	Random r = new Random();
        Comparable[] onDemandInt = new Comparable[length];
        int limit;
        for (limit=0; limit < length; limit++) {
            onDemandInt[limit] = r.nextInt(_INTEGER_BANDWIDTH);
        }
        return onDemandInt;
    }
    
    public Comparable[] generateRandomChar(int length) {
        Random r = new Random();
        Comparable[] onDemandChar = new Comparable[length];
        int limit;
        for (limit=0; limit < length; limit++) {
            onDemandChar[limit] = (char) (r.nextInt(_CHAR_BANDWIDTH) + 'a');
        }
        return onDemandChar;
    }

    public Comparable[] generateRandomString(int length) {
        Random r = new Random();
        Comparable[] onDemandString = new Comparable[length];
        
        int limit;
        int limitString;
        int stringLength;
        
        for (limit=0; limit < length; limit++) {
            stringLength = r.nextInt(_STRING_BANDWIDTH)+1;
            StringBuffer sb = new StringBuffer(stringLength);
            
            for (limitString = 0; limitString < stringLength; limitString++) {
                sb.append((char) (r.nextInt(_CHAR_BANDWIDTH) + 'a'));
            }
            
            onDemandString[limit] = sb.toString();
        }
        return onDemandString;
    }
    
    public Comparable<MuziekCD>[] generateRandomMuziekCD(int length) {
    	Random r = new Random();
        Comparable<MuziekCD>[] onDemandMuziekCD = new Comparable[length];
        
        int limit;
        for (limit=0; limit < length; limit++) {
        	Comparable[] cArtist = generateRandomString(1);
        	Comparable[] cTitle = generateRandomString(1);
        	
        	String artist = (String) cArtist[0];
           	String title = (String) cTitle[0];
        	
        	int year = 1980 + r.nextInt(_INTEGER_CD_BANDWIDTH);
        	
        	MuziekCD m = new MuziekCD();
        	
        	m.setThreadName("foo");
        	m.setArtist(artist);
    		m.setTitle(title);
    		m.setYear(year);
        	
        	onDemandMuziekCD[limit] = (MuziekCD) m;
        }
        return onDemandMuziekCD;
    }
}
